﻿using HtmlAgilityPack; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Common
{
    public static class IPCommon
    {
        //获取客户端ip
        public static string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }

        public static string getClientIpAddress(string ip)
        {
            //https://www.ip.cn/api/index?ip=119.8.62.254&type=1
            string url = @"https://www.ip.cn/api/index";
            string urlstring = "ip="+ ip + "&type=1";
           return HttpHelper.HttpGet(url, urlstring);
        }
        public static string getIp(){
            string url = @"https://2021.ip138.com/";
            var webGet = new HtmlWeb();
            var document = webGet.Load(url);
            var div = document.DocumentNode.SelectNodes("/html/body/p/a");
            var sdiv = div[0].InnerHtml;
  
            string ip = sdiv;
            return ip;
        }

        public static string getIpAddress()
        {
            string url = @"https://2021.ip138.com/";
            var webGet = new HtmlWeb();
            var document = webGet.Load(url);
            var div = document.DocumentNode.SelectNodes("/html/body/p");
            var sdiv = div[0].InnerHtml;
            var arr = sdiv.Split('：');




            string ip = arr[2];
            return ip;
        }
      

    }
}